package sesBean;

import beans.Priority;

import beans.PriorityPK;

import beans.Pulje;
import beans.PuljePK;
import beans.Student;
import beans.Studentsubject;

import beans.StudentsubjectPK;

import beans.Subject;

import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless(name = "SessionEJB", mappedName = "XPProjekt-SessionEJB")
@Remote
@Local
public class SessionEJBBean
  implements SessionEJB, SessionEJBLocal
{
  @PersistenceContext(unitName="Fase2")
  private EntityManager em;

  public SessionEJBBean()
  {
  }

  public Object mergeEntity(Object entity)
  {
    return em.merge(entity);
  }

  public Object persistEntity(Object entity)
  {
    em.persist(entity);
    return entity;
  }

  /** <code>select o from Priority o</code> */
  public List<Priority> queryPriorityFindAll()
  {
    return em.createNamedQuery("Priority.findAll").getResultList();
  }

  /** <code>select o from Priority o</code> */
  public List<Priority> queryPriorityFindAllByRange(int firstResult,
                                                    int maxResults)
  {
    Query query = em.createNamedQuery("Priority.findAll");
    if (firstResult > 0)
    {
      query = query.setFirstResult(firstResult);
    }
    if (maxResults > 0)
    {
      query = query.setMaxResults(maxResults);
    }
    return query.getResultList();
  }

  public void removePriority(Priority priority)
  {
    priority = em.find(Priority.class, new PriorityPK(priority.getStudentid(), priority.getSubjectid()));
    em.remove(priority);
  }

  /** <code>select o from Studentsubject o</code> */
  public List<Studentsubject> queryStudentsubjectFindAll()
  {
    return em.createNamedQuery("Studentsubject.findAll").getResultList();
  }

  /** <code>select o from Studentsubject o</code> */
  public List<Studentsubject> queryStudentsubjectFindAllByRange(int firstResult,
                                                                int maxResults)
  {
    Query query = em.createNamedQuery("Studentsubject.findAll");
    if (firstResult > 0)
    {
      query = query.setFirstResult(firstResult);
    }
    if (maxResults > 0)
    {
      query = query.setMaxResults(maxResults);
    }
    return query.getResultList();
  }

  public void removeStudentsubject(Studentsubject studentsubject)
  {
    studentsubject = em.find(Studentsubject.class, new StudentsubjectPK(studentsubject.getStudentid(), studentsubject.getSubjectid()));
    em.remove(studentsubject);
  }

  /** <code>select o from Pulje o</code> */
  public List<Pulje> queryPuljeFindAll()
  {
    return em.createNamedQuery("Pulje.findAll").getResultList();
  }

  /** <code>select o from Pulje o</code> */
  public List<Pulje> queryPuljeFindAllByRange(int firstResult,
                                              int maxResults)
  {
    Query query = em.createNamedQuery("Pulje.findAll");
    if (firstResult > 0)
    {
      query = query.setFirstResult(firstResult);
    }
    if (maxResults > 0)
    {
      query = query.setMaxResults(maxResults);
    }
    return query.getResultList();
  }

  public void removePulje(Pulje pulje)
  {
    pulje = em.find(Pulje.class, new PuljePK(pulje.getPuljeid(), pulje.getSubjectid()));
    em.remove(pulje);
  }

  /** <code>select o from Subject o</code> */
  public List<Subject> querySubjectFindAll()
  {
    return em.createNamedQuery("Subject.findAll").getResultList();
  }

  /** <code>select o from Subject o</code> */
  public List<Subject> querySubjectFindAllByRange(int firstResult,
                                                  int maxResults)
  {
    Query query = em.createNamedQuery("Subject.findAll");
    if (firstResult > 0)
    {
      query = query.setFirstResult(firstResult);
    }
    if (maxResults > 0)
    {
      query = query.setMaxResults(maxResults);
    }
    return query.getResultList();
  }
  
  public Subject querySubjectFindByID(int iD)
  {
    Query query = em.createNamedQuery("Subject.findByID");
    query.setParameter("subjectId",iD);
    return (Subject)query.getSingleResult();
  }  

  public void removeSubject(Subject subject)
  {
    subject = em.find(Subject.class, subject.getId());
    em.remove(subject);
  }

  /** <code>select o from Student o</code> */
  public List<Student> queryStudentFindAll()
  {
    return em.createNamedQuery("Student.findAll").getResultList();
  }

  /** <code>select o from Student o</code> */
  public List<Student> queryStudentFindAllByRange(int firstResult,
                                                  int maxResults)
  {
    Query query = em.createNamedQuery("Student.findAll");
    if (firstResult > 0)
    {
      query = query.setFirstResult(firstResult);
    }
    if (maxResults > 0)
    {
      query = query.setMaxResults(maxResults);
    }
    return query.getResultList();
  }

  public Student queryStudentFindByID(int iD)
  {
    Query query = em.createNamedQuery("Student.findByID");
    query.setParameter("studentId",iD);
    return (Student)query.getSingleResult();
  }  

  public void removeStudent(Student student)
  {
    student = em.find(Student.class, student.getId());
    em.remove(student);
  }
}
